{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "original data type is  (10000, 28, 28) (10000,)\n",
      "(10000, 10) (10000, 784)\n",
      "INFO:tensorflow:Restoring parameters from ./model\\mininst.ckpt\n"
     ]
    }
   ],
   "source": [
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_v2_behavior()\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy\n",
    "import tensorflow.keras as keras\n",
    "import pandas as pd\n",
    "import argparse \n",
    "\n",
    "(x,y), (x_test,y_test) = keras.datasets.mnist.load_data()  #numpy中的格式\n",
    "print(\"original data type is \",x_test.shape, y_test.shape)    #y为vector\n",
    "x_test = x_test.reshape(10000,784)\n",
    "y_test = pd.get_dummies(y_test)\n",
    "\n",
    "print(y_test.shape,x_test.shape)\n",
    "saver = tf.train.import_meta_graph('./model/mininst.ckpt.meta')\n",
    "# 获取模型参数用于手动构建模型\n",
    "with tf.Session() as sess:\n",
    "    saver.restore(sess, tf.train.latest_checkpoint(\"./model\"))\n",
    "    weights = {\n",
    "        'h1': sess.graph.get_operation_by_name(\"h1\"),\n",
    "        'h2': sess.graph.get_operation_by_name(\"h2\"),\n",
    "        'out': sess.graph.get_operation_by_name(\"outh\")\n",
    "    }\n",
    "    biases = {\n",
    "        'b1': sess.graph.get_operation_by_name(\"b1\"),\n",
    "        'b2': sess.graph.get_operation_by_name(\"b2\"),\n",
    "        'out': sess.graph.get_operation_by_name(\"outb\")\n",
    "    }\n",
    "    init = tf.global_variables_initializer()\n",
    "    sess.run(init)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 784)\n",
      "Tensor(\"Const_22:0\", shape=(10000, 784), dtype=uint8)\n",
      "[7 2 1 ... 4 5 6] [7 2 1 ... 4 5 6]\n",
      "0.8817\n"
     ]
    }
   ],
   "source": [
    "# 使用freeze脚本固化后的模型\n",
    "\n",
    "def load_graph(frozen_graph_filename):\n",
    "    # We parse the graph_def file\n",
    "    with tf.gfile.GFile(frozen_graph_filename, \"rb\") as f:\n",
    "        graph_def = tf.GraphDef()\n",
    "        graph_def.ParseFromString(f.read())\n",
    "\n",
    "    # We load the graph_def in the default graph\n",
    "    with tf.Graph().as_default() as graph:\n",
    "        tf.import_graph_def(\n",
    "            graph_def, \n",
    "            input_map=None, \n",
    "            return_elements=None, \n",
    "            name=\"prefix\", \n",
    "            op_dict=None, \n",
    "            producer_op_list=None\n",
    "        )\n",
    "    return graph\n",
    "\n",
    "#加载已经将参数固化后的图\n",
    "graph= load_graph('frozen_model.pb')\n",
    "# We can list operations\n",
    "#op.values() gives you a list of tensors it produces\n",
    "#op.name gives you the name\n",
    "#输入,输出结点也是operation,所以,我们可以得到operation的名字\n",
    "# for op in graph.get_operations():\n",
    "#     print(op.name,op.values())\n",
    "#操作有:prefix/Placeholder/inputs_placeholder\n",
    "#操作有:prefix/Accuracy/predictions\n",
    "#为了预测,我们需要找到我们需要feed的tensor,那么就需要该tensor的名字\n",
    "#注意prefix/Placeholder/inputs_placeholder仅仅是操作的名字,prefix/Placeholder/inputs_placeholder:0才是tensor的名字\n",
    "x = graph.get_tensor_by_name('prefix/X:0')\n",
    "y = graph.get_tensor_by_name('prefix/output:0')\n",
    "print(x_test.shape)\n",
    "x_tensor = tf.convert_to_tensor(x_test)\n",
    "print(x_tensor)\n",
    "with tf.Session(graph=graph) as sess:\n",
    "    y_out = sess.run(y, feed_dict={\n",
    "        x: x_test\n",
    "    })\n",
    "#     correct_pred = tf.equal(tf.argmax(y_out, 1), tf.argmax(y_test.values, 1))\n",
    "label_test = numpy.argmax(y_out, 1)\n",
    "label = numpy.argmax(y_test.values, 1)\n",
    "\n",
    "print(label_test,label)\n",
    "correct_prediction = numpy.equal(label_test,label)\n",
    "print(numpy.mean(correct_prediction))\n",
    "\n",
    "# 查看错误率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
